package com.example.front;

import com.example.front.arrange.ExecutorHelper;
import com.example.front.arrange.TaskArrangeSupport;
import com.example.front.dao.RecordDao;
import com.example.front.dao.OperationRecordMapper;
import com.example.front.task.CcTask;
import com.example.front.task.DdTask;
import com.example.front.task.EndTask;
import com.example.front.task.AaTask;
import com.example.front.task.BbTask;
import com.example.front.task.FfTask;
import com.example.front.task.RideContext;
import com.example.front.task.Start1Task;
import com.example.front.task.Start2Task;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
//@PropertySource("classpath:dynamic.properties")
//@EnableDynamicTp
@EnableTransactionManagement
@SpringBootApplication
//@EnableTaskArrange(RideContext.class)
public class FrontApplication implements ApplicationRunner {

    Logger logger = LoggerFactory.getLogger(FrontApplication.class);
    private static SpringApplication application;

    public static void main(String[] args) {
        log.info("boot args:{}", Arrays.toString(args));
        application = new SpringApplication(FrontApplication.class);
        application.addListeners(new ApplicationPidFileWriter("app.pid"));
        application.run(args);
    }

    //@Value("${app.id.aaaaa}")
    private String aaa;
    //@Value("${spring.profiles.active}")
    private String bbb;


    @Autowired
    private OperationRecordMapper operationRecordMapper;
    @Autowired
    private TaskArrangeSupport taskSupport;
    @Autowired
    private RecordDao recordDao;
    private ThreadPoolExecutor executor = ExecutorHelper.getInstance().getExecutor("testThreadPool");
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //recordDao.f1();
        int a = 1;
        while (a-- > 0) {
            Start1Task start1Task = new Start1Task("start1");
            Boolean result1 = taskSupport.runTaskAsync(start1Task, executor);
            logger.info("result1 wait result1:{}", result1);
            long start = System.currentTimeMillis();
            ExecutorHelper.getInstance().execute("wait1", () -> logger.info("result1 EndTask:{} cost:{}", start1Task.getResultOfTask(EndTask.class), System.currentTimeMillis() - start));
            ExecutorHelper.getInstance().execute("wait1", () -> logger.info("result1 DdTask:{} cost:{}", start1Task.getResultOfTask(DdTask.class), System.currentTimeMillis() - start));
            ExecutorHelper.getInstance().execute("wait1", () -> logger.info("result1 AaTask:{} cost:{}", start1Task.getResultOfTask(AaTask.class), System.currentTimeMillis() - start));

            //RideContext context = new RideContext();
            //context.setId("start2");
            //Start2Task start2Task = new Start2Task(context);
            //Boolean result2 = taskSupport.runArrangeTask(start2Task);
            //logger.info("result2:{},{},{}", start2Task.getResultOfTask(BbTask.class), start2Task.getResultOfTask(CcTask.class), start2Task.getResultOfTask(FfTask.class));
            //logger.info("result2 context:{}", start2Task.getResultOfTask(Start2Task.class));
        }
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
